﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Client;
using T.FAS.Runtime.Metadata.SPI;

namespace T.FAS.Runtime.DataModel.Plugin
{
    public class DataModelRTPClient
    {
        private static readonly object s_lockObj = new object();
        private static DataModelRTPClient s_inst;
        public static DataModelRTPClient Instance
        {
            get
            {
                if (s_inst == null)
                {
                    lock (s_lockObj)
                    {
                        if (s_inst == null)
                        {
                            s_inst = new DataModelRTPClient();
                        }
                    }
                }
                return s_inst;
            }
        }

        /// <summary>
        /// ttc已经依赖默认构造函数，暂保持public
        /// </summary>
        public DataModelRTPClient() { }

        private ColumnPermissionViewDao _columnPermissionViewDao = new ColumnPermissionViewDao();

        /// <summary>
        /// 获取CDO的数据库信息
        /// </summary>
        /// <param name="cdoID"></param>
        /// <returns></returns>
        public CDODBInfo GetCDODBInfo(string cdoID)
        {
            var cdo = DataModelRTPUtil.GetCompositeDataObject(cdoID);
            try
            {
                var rootDO = cdo.RootCompositeNodeObject.DataObject;
                var db = rootDO.Database;
                var childNodes = cdo.CompositeNodeObjects.Where(cno => cno.ParentObjectID == cdo.RootCompositeNodeObjectID);
                CDODBInfo info = new CDODBInfo();
                info.RootTableName = rootDO.LogicTableName;
                info.DBName = db.Code;
                info.DBConfigKey = db.DatabaseConfigs.FirstOrDefault(cfg => cfg.IsDefault).ConfigKey;
                info.ChildTables = childNodes.Select(node => node.DataObject.LogicTableName).ToList();
                return info;
            }
            catch (Exception e)
            {
                LocalLogService.WriteError(e, "运行时获取CDO数据库信息失败");
                throw new FasException(DataModelErrorCode.RTP.Error, $"运行时获取CDO数据库信息失败", e);
            }
        }

        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="doIds"></param>
        /// <param name="authSQL"></param>
        /// <returns></returns>
        public List<ColumnPermissionView> GetColumnPermissionViews(List<string> doIds, string authSQL)
        {
            return _columnPermissionViewDao.GetColumnPermissionViews(doIds, authSQL);
        }

        public List<ColumnPermissionView> GetAllColumnPermissionViews(string authSQL)
        {
            return _columnPermissionViewDao.GetAllColumnPermissionViews(authSQL);
        }
    }

    public class CDODBInfo
    {
        public string RootTableName { get; set; }
        public string DBConfigKey { get; set; }
        public string DBName { get; set; }
        public List<string> ChildTables { get; set; }
    }
}
